package com.shudes.util;

import java.math.*;

abstract public class DiscreteUtils {

	protected DiscreteUtils() {
	}
	
	public static BigInteger big(Number n) { 
		return (n==null) ? null : new BigInteger(n.toString()); 
	}
	
	public static BigInteger big(BigInteger i) { 
		return new BigInteger(i.toString()); 				
	}
	
	public static BigInteger bigZero() { 
		return new BigInteger("0"); 
	}
	
	public static BigInteger factorial(Number n) { 
		return factorial(big(n)); 
	}
	
	public static BigInteger factorial(BigInteger n) { 
		BigInteger b; 
				
		if(n==null) 
			return null;
		
		if(n.longValue()==0)
			return bigZero();
		
		b = big(n); 
		
		return (n.longValue()>1) ? b.multiply(factorial(big(n).subtract(big(1)))) : n;		
	}
	
	public static BigInteger combinations(BigInteger n, BigInteger r) { 
		return (n.equals(r)) ? big(1) : factorial(n).divide(factorial(r)).divide(factorial(n.subtract(r))); 
	}
	
	public static BigInteger combinations(Number n, Number r) { 
		return combinations(big(n), big(r)); 
	}

}
